<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>

<ul class="breadcrumb panel">
    <li><a href="javascript:backMainFun();"><i class="fa fa-home"></i> 首页</a></li>
    <li><a href="javascript:loadFun('/user/toUserIndex');">用户管理</a></li>
    <li class="active">新增用户</li>
</ul>

<div class="panel">
    <header class="panel-heading">
        新增用户
    </header>
    <div class="panel-body">
        <form action="${ctx}/user/doSave" name="userForm" id="userForm" method="post" class="form-horizontal">
            <div class="form-group">
                <label for="login_name" class="col-sm-2 control-label must">登录名</label>
                <div class="col-sm-8">
                    <input type="text" class="form-control" id="login_name" name="login_name" placeholder="请输入登录名">
                </div>
            </div>
            <div class="form-group">
                <label for="user_name" class="col-sm-2 control-label must">用户名</label>
                <div class="col-sm-8">
                    <input type="text" class="form-control" id="user_name" name="user_name" placeholder="请输入用户名">
                </div>
            </div>
            <div class="form-group">
                <label for="id_card" class="col-sm-2 control-label must">身份证号</label>
                <div class="col-sm-8">
                    <input type="text" class="form-control" id="id_card" name="id_card" placeholder="请输入身份证号" maxlength="18">
                </div>
            </div>
            <div class="form-group has-feedback">
                <label for="org_name" class="col-sm-2 control-label must">组织机构</label>
                <div class="col-sm-8">
                    <input type="hidden" id="org_id" value="">
                    <input type="text" class="form-control" id="org_name" name="org_name" placeholder="请选择所属部门">
                    <i class="fa fa-sitemap form-control-feedback"></i>
                </div>
            </div>
            <div class="form-group" style="display: none">
                <label for="permission" class="col-sm-2 control-label must">可视范围</label>
                <div class="col-sm-8">
                    <select class="form-control" id="permission" name="permission" placeholder="请选择可视范围">
                        <c:forEach var="t" items="${pmList}">
                            <option value="${t.code}">${t.value}</option>
                        </c:forEach>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label for="phone" class="col-sm-2 control-label must">联系电话</label>
                <div class="col-sm-8">
                    <input type="text" class="form-control" id="phone" name="phone" placeholder="请输入联系电话">
                </div>
            </div>
            <div class="form-group">
                <label for="email" class="col-sm-2 control-label">邮箱</label>
                <div class="col-sm-8">
                    <input type="text" class="form-control" id="email" name="email" placeholder="请输入邮箱">
                </div>
            </div>
            <div class="form-group">
                <label for="remark" class="col-sm-2 control-label">备注</label>
                <div class="col-sm-8">
                    <input type="text" class="form-control" id="remark" name="remark" placeholder="···">
                </div>
            </div>
            <div class="row">
                <div class="col-xs-10">
                    <button type="button" class="btn btn-primary pull-right" id="subBtn"><i
                            class="glyphicon glyphicon-saved"></i> 保存
                    </button>
                    <button type="button" class="btn btn-default pull-right marR" onclick="backFun()"><i
                            class="glyphicon glyphicon-chevron-left"></i> 返回
                    </button>
                </div>
            </div>

        </form>
    </div>
</div>

<div class="modal fade" id="orgModal">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title">选择组织机构</h4>
            </div>
            <div class="modal-body">
                <div class="container-fluid" style="background: #f8f8f8;border-radius: 3px;max-height: 500px;overflow: auto">
                    <div id="treeApp" class="ztree"></div>
                </div>

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal"><i class="glyphicon glyphicon-off"></i> 关闭</button>
                <button id="checkorgOk" type="button" class="btn btn-primary"><i class="glyphicon glyphicon-ok"></i> 确定</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<script>
    function backFun() {
        loadFun("/user/toUserIndex");
    }

    $('#login_name').blur(function () {
        if ($('#login_name').val() != false) {
            $.post("${ctx}/user/checkName",
                    {
                        login_name: $('#login_name').val()
                    }
                    , function (data) {
                        if (!data.valid) {
                            zAlert("登录名已被占用，请重新填写!");
                            $('#login_name').focus();
                            $('#subBtn').attr("disabled",true);
                        }else{
                            $('#subBtn').attr("disabled",false);
                        }
                    });
        }

    });


    $('#subBtn').click(function () {
        var regPhone=/^\d+$/;
        if ($('#login_name').val() == false) {
            zAlert('登录名不能为空')
            return false;
        } else if ($('#user_name').val() == false) {
            zAlert('用户名不能为空')
            return false;
        } else if ($('#id_card').val() == false) {
            zAlert('身份证号不能为空')
            return false;
        } else if ($('#org_name').val() == false) {
            zAlert('组织机构不能为空')
            return false;
        } else if ($('#phone').val() == false) {
            zAlert('联系电话不能为空')
            return false;
        } else if (!regPhone.test($('#phone').val())) {
            zAlert('联系电话输入不正确')
            return false;
        } else {
            $.ajax({
                url: "${ctx}/user/doSave",
                type: 'post',
                data: {
                    login_name: $('#login_name').val(),
                    user_name: $('#user_name').val(),
                    id_card:$('#id_card').val(),
                    org_name: $('#org_name').val(),
                    org_id:$('#org_id').val(),
                    phone: $('#phone').val(),
                    email: $('#email').val(),
                    permission:$('#permission').val(),
                    remark: $('#remark').val()
                },
                success: function (data) {
                    if (data.success) {
                        zAlert(data.msg);
                        setTimeout(function () {
                            loadFun("/user/toUserIndex");
                        }, 1000);
                    } else {
                        zAlert(data.msg);
                    }
                }
            })
        }
    });


    //   部门组织树及选择部门代码
    var zTreeObj;
    var zNodes;
    function  filter(treeId, parentNode, data) {
        if (!data) return null;
        var childNodes = [];
        childNodes = data;

        for(var i=0;i<childNodes.length;i++){
            childNodes[i].isParent = true;
        };

        return childNodes;
    };

    $.post('${ctx}/org/getOrgTree',{
        parentId:'',
    },function (data) {
        var setting = {
            view: {
                showLine: false,
                expandSpeed:'fast'
            },
            async: {
                enable: true,
                contentType: "application/x-www-form-urlencoded",
                url: "${ctx}/org/getOrgTree",
                dataType: "text",
                type:'post',
                autoParam: ["org_id=parentId"],
                dataFilter: filter
            },
            check:{
                enable: true,
                chkStyle: "radio",
                radioType: "all"
            }
        };

        zNodes = data;
        zNodes[0].open=true;
        zNodes[0].isParent = true;

        for(var i=0;i<zNodes[0].children.length;i++){
            zNodes[0].children[i].isParent = true;
        };

        zTreeObj = $.fn.zTree.init($("#treeApp"), setting, zNodes);
    });
    //部门组织树及选择部门代码 end




    $('#org_name').focus(function(){
        $('#orgModal').modal('show');
        $(this).blur();
    });
    $('#checkorgOk').click(function(){
        var nodes=zTreeObj.getCheckedNodes(true);
        $('#org_name').val(nodes[0].full_name);
        $('#org_id').val(nodes[0].org_id);
        $('#orgModal').modal('hide');
    });


    //部门组织树及选择部门代码 end

    function IdentityCodeValid(code) {
        var city={11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙江 ",31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖北 ",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",54:"西藏 ",61:"陕西",62:"甘肃",63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外 "};
        var tip = "";
        var pass= true;

        if(!code || !/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[12])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/i.test(code)){
            tip = "身份证号格式错误";
            pass = false;
        }

        else if(!city[code.substr(0,2)]){
            tip = "身份证地址编码错误";
            pass = false;
        }
        else{
            //18位身份证需要验证最后一位校验位
            if(code.length == 18){
                code = code.split('');
                //∑(ai×Wi)(mod 11)
                //加权因子
                var factor = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 ];
                //校验位
                var parity = [ 1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2 ];
                var sum = 0;
                var ai = 0;
                var wi = 0;
                for (var i = 0; i < 17; i++)
                {
                    ai = code[i];
                    wi = factor[i];
                    sum += ai * wi;
                }
                var last = parity[sum % 11];
                if(parity[sum % 11] != code[17]){
                    tip = "身份证校验位错误";
                    pass =false;
                }
            }
        }
        if(!pass) zAlert(tip,'danger');
        return pass;
    }

</script>

